package com.upc.toss.fraction;

import com.upc.toss.ResourceTable;
import com.upc.toss.util.Get;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class MainFraction extends Fraction {
    Text textTemperature;
    Text textHumanity;
    Text textLuminance;
    Text textLightStatus;
    Text textMotorStatus;
    private static final String TAG="MainFraction";
    private int MY_MODULE=0x00201;
    private final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, MY_MODULE, TAG); //
    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container,
                                            Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_fraction_main, container, false);

        textTemperature=(Text) component.findComponentById(ResourceTable.Id_text_temperature);
        textHumanity=(Text) component.findComponentById(ResourceTable.Id_text_humidity) ;
        textLuminance=(Text) component.findComponentById(ResourceTable.Id_text_illumination) ;
        textLightStatus=(Text) component.findComponentById(ResourceTable.Id_text_lightStatus);
        textMotorStatus=(Text) component.findComponentById(ResourceTable.Id_text_motorStatus);
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        EventRunner runner = EventRunner.current();
        MyEventHandler handler = new MyEventHandler(runner);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    Get.getProperties();
                    handler.sendEvent(1);
                    try {
                        Thread.sleep(2000);
                        System.out.println("updating!");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        handler.sendEvent(1);
    }


    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData){

    }
    @Override
    protected void onActive() {
        super.onActive();
    }
    @Override
    protected void onInactive() {
        super.onInactive();
    }
    @Override
    protected void onBackground() {
        super.onBackground();
    }
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
    }


    private class MyEventHandler extends EventHandler {
        private MyEventHandler(EventRunner runner) {
            super(runner);
        }
        // 重写实现processEvent方法
        @Override
        public void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event == null) {
                return;
            }
            int eventId = event.eventId;
            switch (eventId) {
                case 1:
                    textTemperature.setText(Get.temperature);
                    textHumanity.setText(Get.humidity);
                    textLuminance.setText(Get.luminance);
                    textLightStatus.setText(Get.lightStatus);
                    textMotorStatus.setText(Get.motorStatus);
                    break;
                case 2:
                    System.out.println("主线程2");
                    break;
                default:
                    break;
            }
        }
    }


}

